package work92;

/**
 * @author chenmin
 * @desc TODO
 * @date 2025/9/2
 */

// 基类 Person
class Person {
    protected String name;
    protected int age;
    protected boolean gender; // true:男, false:女
    protected Person partner; // 伴侣

    // 构造函数
    public Person(String name, int age, boolean gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.partner = null;
    }

    // 结婚方法
    public void marry(Person p) {
        // 检查是否同性
        if (this.gender == p.gender) {
            System.out.println(this.name + " 和 " + p.name + " 不能结婚：同性不能结婚");
            return;
        }

        // 检查男方年龄是否达到24岁
        Person man = this.gender ? this : p;
        Person woman = this.gender ? p : this;

        if (man.age < 24) {
            System.out.println(man.name + " 年龄未达到24岁，不能结婚");
            return;
        }

        // 检查女方年龄是否达到22岁
        if (woman.age < 22) {
            System.out.println(woman.name + " 年龄未达到22岁，不能结婚");
            return;
        }

        // 检查某一方是否已婚
        if (this.partner != null) {
            System.out.println(this.name + " 已经结婚，不能再次结婚");
            return;
        }

        if (p.partner != null) {
            System.out.println(p.name + " 已经结婚，不能再次结婚");
            return;
        }

        // 可以结婚
        this.partner = p;
        p.partner = this;
        System.out.println("恭喜 " + this.name + " 和 " + p.name + " 喜结连理！");
    }

    // 获取婚姻状态
    public String getMaritalStatus() {
        return partner == null ? "未婚" : "已婚";
    }

    // 显示信息
    public void displayInfo() {
        System.out.println("姓名: " + name + ", 年龄: " + age + ", 性别: " +
                (gender ? "男" : "女") + ", 婚姻状态: " + getMaritalStatus());
    }
}

// 继承Person类的子类
class Adult extends Person {
    public Adult(String name, int age, boolean gender) {
        super(name, age, gender);
    }
}

// 测试类
public class MarriageTest {
    public static void main(String[] args) {
        // 创建测试对象
        Adult man1 = new Adult("张三", 25, true);
        Adult woman1 = new Adult("李四", 23, false);
        Adult man2 = new Adult("王五", 22, true); // 年龄不够
        Adult woman2 = new Adult("赵六", 21, false); // 年龄不够
        Adult man3 = new Adult("孙七", 26, true);
        Adult woman3 = new Adult("周八", 24, false);

        System.out.println("=== 测试用例1: 正常结婚 ===");
        man1.marry(woman1);

        System.out.println("\n=== 测试用例2: 男方年龄不够 ===");
        man2.marry(woman3);

        System.out.println("\n=== 测试用例3: 女方年龄不够 ===");
        man3.marry(woman2);

        System.out.println("\n=== 测试用例4: 同性结婚 ===");
        man1.marry(man3);

        System.out.println("\n=== 测试用例5: 重复结婚 ===");
        man1.marry(woman3); // 张三已经结婚

        System.out.println("\n=== 个人信息展示 ===");
        man1.displayInfo();
        woman1.displayInfo();
        man2.displayInfo();
        woman2.displayInfo();
    }
}